GBML manual / Queues

queue_create

Makes an empty queue that can hold up to capacity items. example is any value of the kind it will hold - queue_create(16, 0) is a queue - and is never put in the queue. capacity must be a whole number written right there in the code, and a power of two (2, 4, 8, 16, 32...).

Syntax

queue_create(capacity, example) -> queue<T>

Arguments

NameType
capacityint
exampleelement

Returns

queue<T>

Description

Makes an empty queue that can hold up to capacity items. queue_create(8, 0) is a queue of whole numbers with room for 8.

capacity is the most the queue will ever hold, and it has to be a power of two: 2, 4, 8, 16, 32 and so on, never 10 or 30. That rule comes from the container the Game Boy Advance builds a queue out of, and it is the same whichever way you write the size. If you pick another number, the compiler tells you the next size up.

It also has to be a number you type, like 8, not a variable, because a GBA cannot hand out memory while a game runs.

example tells GBML what kind of thing waits in the queue. Give any value of that kind: 0 for whole numbers, 0.0 for decimals, "" for text, true for yes-or-no. The queue starts empty, and the example is never added to it. It is only read to learn the kind.

It sets the size of the variable it is given to, so it goes on the right-hand side of the line that creates the variable: var turns = queue_create(8, 0);.

Leave the size out and you get 8.

queue_create(8, 0) means the same as var turns: queue<int, 8> = []; and the /// @capacity 8 comment. See list_create for the three ways side by side.

Example

var turns = queue_create(8, 0);     // an empty queue of whole numbers
queue_push(turns, 1);
queue_push(turns, 2);
var next = queue_pop(turns);        // 1 - the first one in is the first one out

On the GBA

Compiles to an empty fixed-size double-ended queue of `capacity` items, the same storage `/// @type {queue<T>}` + `/// @capacity capacity` gives. A double-ended queue only compiles with a power-of-two size (the container checks this when the game is built), so any other number is refused and the next power of two up is suggested. Leave the size out and a queue holds 8. The size has to be a plain number because it becomes a C++ template parameter: the Game Boy Advance has no heap to hand out memory while the game runs, so every container is sized when the ROM is built (anything else is refused). It must be the whole right-hand side of a declaration, because that is the one place a size can be attached to a variable. The example is only read for its kind - it is never evaluated, never stored and never put in the container.

See also: queue_pushqueue_popqueue_copylist_createstack_createQueuesWriting a type out, instead of in a commentData containers